<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="部门名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getname">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="Add">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="power" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">

      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="部门名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="180" :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column prop="intro" label="介绍" width="180" sortable>
      </el-table-column>
      <el-table-column prop="manager.username" label="部门经理" width="180" sortable>
      </el-table-column>
      <el-table-column prop="parent.name" label="上级部门" min-width="250" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  分页条  :disabled="this.sels.length===0"-->
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pagesizes"
          :pagesize="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!-- 弹出框 dialogFormVisible -->`
      <el-dialog :title="title" :visible.sync="dialogFormVisible" >
        <el-form :model="deptForm" label-width="80px" :rules="deptFormRules" ref="deptForm">
          <el-form-item label="部门名称" prop="name">
            <el-input v-model="deptForm.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="部门简介">
            <el-input  v-model="deptForm.intro"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="deptForm.state">
              <el-radio class="radio" :label="1">启用</el-radio>
              <el-radio class="radio" :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="部门经理">
<!--            <el-input  v-model="deptForm.manager_id"></el-input>-->

            <el-select v-model="deptForm.manager" value-key="id" clearable placeholder="请选择部门经理">
              <el-option
                  v-for="item in emps"
                  :key="item.id"
                  :label="item.username"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上级部门">
<!--            <el-input  v-model="deptForm.parent_id"></el-input>-->
            <el-cascader
                v-model="deptForm.parent"
                :options="depttree"
                :props="{
                  checkStrictly:true,
                  label:'name',
                  value:'id'
                }">

            </el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="formVisible = false">取消</el-button>
          <el-button type="primary" @click.native="formSubmit" :loading="deptFormLoading">提交</el-button>
        </div>
      </el-dialog>
    </div>
<!--TypeError-->
  </section>
</template>



    <script>

      export default{
        name:"Manages",
        data(){
          return{
            filters:{
              name:""
            },
            power:[], //显示每条信息的数组
            listLoading: false,//页面加载的效果
            //     分页条
            total:100,//一共有多少条
            pagesizes:[5, 10, 20, 100],//每页显示的条数
            pageSize:10,//默认每页显示的条数
            currentPage:1,//当前页面

            title:"新增",//标题
            dialogFormVisible:false, //弹出框
            deptForm:{//表格
              id:null,
              name:null,
              intro:null,
              manager_id:null,
              parent_id:null,
              state:null,
              path:null,
              manager:null,
              parent:null
            },
            deptFormRules: {
              name: [
                { required: true, message: '请输入部门名称', trigger: 'blur' }
              ]
            },
            deptFormLoading:false ,
            emps:[],
            value:'',
            depttree:[],
            sels:[],

          }


        },
        methods:{
          getname() {//高级查询
            this.findq2()
          },
          Add(){//新增
            this.deptForm={//清空表单数据
              id:null,
              name:null,
              intro:null,
              manager_id:null,
              parent_id:null,
              state:null,
              path:null,
              manager:null,
              parent:null
            }
            this.findtree()
            this.dialogFormVisible=true; //打开弹出框

            // console.log(this.formVisible)


          },
          selsChange: function (sels) {//不知道
            this.sels = sels;
          },

          formatSex: function (row, column) {
            return row.state == 1 ? '启用' : '禁用';
          },
          handleSizeChange(val) {//每页几条
            this.pageSize=val
            this.findq2()
            // console.log(this.pageSize+"  "+val);
          },
          handleCurrentChange(val) {//当前是多少页
            this.currentPage=val
            this.findq2()
            // console.log(this.currentPage4+`当前页: ${val}`);
          },
          handleEdit(index,row){//编辑

            Object.assign(this.deptForm,row);//做数据的回显

            this.findtree()
            this.deptForm.parent=row.parent.id//数据回显
            this.dialogFormVisible=true; //打开弹出框

          },
          handleDel(index,row){//删除
            this.$confirm('确认删除数据吗？', '提示', {
              type: 'warning'
            }).then(() => {
              //确认框 用户点击了确认
              this.$http.delete("/dept/"+row.id).then((res) => {

                if(res.data.success){
                  this.$message({
                    message: '删除成功',
                    type: 'success'
                  });
                  this.findq2()
                }else {
                  this.$message.error(res.data.message);
                }

              })
            })
          },
          batchRemove(){//批量删除
            var ids = this.sels.map(item => item.id);
            this.$confirm('确认删除选中记录吗？', '提示', {
              type: 'warning'
            }).then(() => {
              this.listLoading = true;
              //NProgress.start();
              let para = { ids: ids };
              this.$http.patch("/dept",para).then((res) => {
                this.listLoading = false;
                //NProgress.done();
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.findq2();
              });
            }).catch(() => {

            });
          },
          formSubmit(){//提交
            this.$refs.deptForm.validate((valid) => {
                  if (valid) {//通过表单验证
                    this.$confirm('确认提交数据吗？', '提示', {}).then(() => {
                      //确认框 用户点击了确认
                      this.deptFormLoading = true;

                      if (this.deptForm.parent){
                        let index = this.deptForm.parent.length;
                        this.deptForm.parent={
                          id:this.deptForm.parent[index-1]
                        }
                      }


                      this.$http.put("/dept",this.deptForm).then((res) => {

                        if(res.data.success){
                          this.$message({
                            message: '提交成功',
                            type: 'success'
                          });
                          this.findq2()
                        }else {
                          this.$message.error(res.data.message);
                        }
                        //关闭弹出框
                        this.dialogFormVisible=false
                        this.findq2()
                        this.deptFormLoading = false

                      });
                    });
                  }
               })

          },
          findmanager(){//部门经理查询
            this.$http.get("/dept/findmanager").then((res) => {
              console.log(res.data)
              this.emps = res.data
            })
          },
          findtree(){//部门
            this.$http.get("/dept/tree").then((res) => {
              console.log(res.data)
              this.depttree = res.data
            })
          },

          findq2(){
            this.listLoading= true//加载的转的圈
            let page={
              currentPage:this.currentPage,
              pageSize:this.pageSize,
              name:this.filters.name
            }
            this.$http.post("/dept",page).then(res=>{
              // console.log(res.data)
              this.power=res.data.data

              this.total=res.data.count
              this.listLoading= false
            })
          },

        },
          mounted() {
            this.findq2()
            this.findmanager()
          }
      }


    </script>


<style scoped>

</style>